package ink.lovejinhu.leetcode;

/**
 * @author jinhu
 * created 2021-07-15 9:51
 */
public class Solution_0414 {
    public static void main(String[] args) {
        Solution_0414 solution_0414 = new Solution_0414();
        int[] test = {1, 2, 2, 3};
        solution_0414.thirdMax(test);
        System.out.println(solution_0414.thirdMax(test));
    }

    /**
     * 第三大的数
     *
     * @param nums
     * @return
     */
    public int thirdMax(int[] nums) {
        long[] ints = new long[3];
        for (int i = 0; i < 3; i++) { //0存最小的

            ints[i] = Long.MIN_VALUE;
        }
        for (int i = 0; i < nums.length; i++) {
            this.getRes(ints, nums[i]);
        }

        if (ints[0] == Long.MIN_VALUE) return (int) ints[2];
        return (int) ints[0];
    }

    public void getRes(long[] param, int n) {
        for (int i = 2; i >= 0; i--) {
            if (param[i] < n) {
                long temp = param[i];
                for (int j = i - 1; j >= 0; j--) {
                    long t = param[j];
                    param[j] = temp;
                    temp = t;
                }
                param[i] = n;
                break;
            } else if (param[i] == n) {
                break;
            }
        }
    }
}
